package com.jic.market.impl.rest.promotion.promotionInfoHandler;

import com.beust.jcommander.internal.Lists;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.utils.DateUtils;
import com.jic.market.entity.PromotionEntity;
import com.jic.market.entity.base.BaseActivity;
import com.jic.market.entity.skill.ActivitySkillGoodsConfig;
import com.jic.market.enums.PromotionStatusEnum;
import com.jic.market.enums.PromotionTypeEnum;
import com.jic.market.mapper.base.BaseActivityMapper;
import com.jic.market.mapper.skill.ActivitySkillGoodsConfigMapper;
import com.jic.market.request.rest.promotion.PromotionInfoRestRequest;
import com.jic.market.response.rest.promotion.PromotionDetailRestResponse;
import com.jic.market.response.rest.promotion.PromotionGoodsSpuRestResponse;
import com.jic.market.service.base.BaseActivityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;


/**
 * description: SkillHandler
 * date: 2020/4/8 16:38
 * author: wangys
 * version: 1.0
 * 秒杀
 */
@Service
@Slf4j
public class SkillHandler extends BaseInfoHandlerTemplate {

    @Autowired
    ActivitySkillGoodsConfigMapper activitySkillGoodsConfigMapper;

    @Autowired
    BaseActivityMapper baseActivityMapper;

    @Autowired
    BaseActivityService baseActivityService;

    final static String SKILL_PROM_TYPE = PromotionTypeEnum.SECKILL.getCode() + "";

    final static String SKILL_ING_STSTUS = PromotionStatusEnum.ING.getCode() + "";

    @Override
    public List<Integer> frontPromTypes() {
//        return Lists.newArrayList(
//                FrontPromotionTypeEnum.SECKILL_SECKILL
//        ).stream().map(FrontPromotionTypeEnum::getCode).collect(Collectors.toList());
        return Lists.newArrayList();
    }

    @Override
    public boolean enable() {
        return false;
    }

    @Override
    public String handlerCode() {
        return "skill_handler";
    }

    @Override
    public List<PromotionDetailRestResponse> apply(PromotionInfoRestRequest request) {
        final List<PromotionDetailRestResponse> promotions = new ArrayList<>();
        //关联查询goods-spu和market_activity_fullnum_goods_config--promotionIds
        List<String> promotionCodes = activitySkillGoodsConfigMapper.queryAllPromotionIdByThirdProductCode(request.getThirdProductCode());
        // 查询出进行中的活动 然后根据promotionCodes过滤
        BaseActivity activity = new BaseActivity();
        activity.setPromType(SKILL_PROM_TYPE);
        activity.setStatus(SKILL_ING_STSTUS);
        activity.setDeleteFlag(0);
        //activity.setDeptType();
        Page page = new Page();
        page.setStart(1);
        page.setLimit(Integer.MAX_VALUE);
        PageResult<BaseActivity> result = baseActivityService.querySkillPage(activity, page,null,null);
        List<BaseActivity> activities = result.getRows();
        if(CollectionUtils.isEmpty(activities)){
            return promotions;
        }
        //组装返回值
        activities.stream()
                .filter(x->{return promotionCodes.contains(x.getPromotionId());})
                .sorted(Comparator.comparing(BaseActivity::getCreateTime).reversed())
                .forEach(a->{
                    promotions.add(genPromotionDetailRestResponse(a));
                });
        return promotions;
    }

    private PromotionDetailRestResponse genPromotionDetailRestResponse(BaseActivity activity){
        return new PromotionDetailRestResponse()
                .setEndTime(activity.getEndTime().getTime())
                .setStartTime(activity.getStartTime().getTime())
                .setPromotionCode(activity.getPromotionId())
                .setPromotionType(PROM_TYPE_MAP.get(activity.getPromType()))
                .setFrontPromotionType(FRONT_PROM_TYPE_MAP.get(activity.getFrontPromType()))
                .setStartTimeStr(DateUtils.getFormatDateTime(activity.getStartTime()))
                .setEndTimeStr(DateUtils.getFormatDateTime(activity.getEndTime()))
                .setDescription(genDescription(activity));
    }

    private String genDescription(BaseActivity activity){
        return activity.getChineseDesc();
    }


    @Override
    public PromotionDetailRestResponse promotionDetail(PromotionInfoRestRequest request) {
        return genPromotionDetailRestResponse(baseActivityMapper.queryByPromotionId(request.getPromotionCode()));
    }

    @Override
    public List<PromotionEntity> promotionBaseDetail(PromotionInfoRestRequest request) {
        List<PromotionEntity> promotionEntities = new ArrayList<>();
        //关联查询goods-spu和market_activity_fullnum_goods_config--promotionIds
//        List<String> promotionCodes = activitySkillGoodsConfigMapper.queryAllPromotionIdByThirdProductCode(request.getThirdProductCode());
//        // 查询出进行中的活动 然后根据promotionCodes过滤
//        BaseActivity activity = new BaseActivity();
//        activity.setPromType(SKILL_PROM_TYPE);
//        activity.setStatus(Integer.parseInt(SKILL_ING_STSTUS));
//        activity.setDeleteFlag(0);
//        //activity.setDeptType();
//        Page page = new Page();
//        page.setStart(1);
//        page.setLimit(Integer.MAX_VALUE);
//        PageResult<BaseActivity> result = baseActivityService.querySkillPage(activity, page,null,null);
//        List<BaseActivity> activities = result.getRows();
//        if(CollectionUtils.isEmpty(activities)){
//            return promotionEntities;
//        }
//        activities.stream()
//                .filter(x->{return promotionCodes.contains(x.getPromotionId());})
//                .forEach(a->{
//                    PromotionEntity promotionEntity = new PromotionEntity();
//                    promotionEntity.setPromotionCode(a.getPromotionId());
//                    promotionEntity.setPromType(PROM_TYPE_MAP.get(a.getPromType()));
//                    promotionEntity.setFrontPromType(FRONT_PROM_TYPE_MAP.get(a.getFrontPromType()));
//                    promotionEntity.setCreateTime(a.getCreateTime());
//                    promotionEntities.add(promotionEntity);
//                });
        return promotionEntities;
    }

    @Override
    public PromotionEntity getPromotionDetailByPromotionCode(String promotionCode) {
        PromotionEntity promotionEntity = null;
        BaseActivity baseActivity = baseActivityMapper.queryByPromotionId(promotionCode);
        if(baseActivity != null){
            promotionEntity = new PromotionEntity();
            promotionEntity.setPromotionCode(baseActivity.getPromotionId());
            promotionEntity.setPromType(PROM_TYPE_MAP.get(baseActivity.getPromType()));
            promotionEntity.setFrontPromType(FRONT_PROM_TYPE_MAP.get(baseActivity.getFrontPromType()));
            promotionEntity.setCreateTime(baseActivity.getCreateTime());
            promotionEntity.setStatus(Integer.parseInt(baseActivity.getStatus()));
            promotionEntity.setStartTime(baseActivity.getStartTime());
            promotionEntity.setEndTime(baseActivity.getEndTime());
        }
        return promotionEntity;
    }

    @Override
    public PageResult<PromotionGoodsSpuRestResponse> getGoodsByPromotionCode(PromotionInfoRestRequest request, Long promotionId) {
        List<PromotionGoodsSpuRestResponse> spuRestResponseList = new ArrayList<>();
        int startIndex = (request.getStart() - 1) * request.getLimit();
        ActivitySkillGoodsConfig activitySkillGoodsConfig = new ActivitySkillGoodsConfig();
        activitySkillGoodsConfig.setDeleteFlag(false);
        activitySkillGoodsConfig.setPromotionId(request.getPromotionCode());
        activitySkillGoodsConfig.setSpuId("0");
        List<ActivitySkillGoodsConfig> list = activitySkillGoodsConfigMapper.queryPage(startIndex, request.getLimit(), activitySkillGoodsConfig);
        long count = activitySkillGoodsConfigMapper.queryPageCount(activitySkillGoodsConfig);
        if(CollectionUtils.isNotEmpty(list)){
            for (ActivitySkillGoodsConfig skillGoodsConfig : list) {
                spuRestResponseList.add(
                        new PromotionGoodsSpuRestResponse()
                        .setGoodsName(skillGoodsConfig.getGoodsName())
                        .setThirdProductCode(skillGoodsConfig.getGoodsCode())
                );
            }
        }
        PageResult pageResult = new PageResult();
        pageResult.setRows(spuRestResponseList);
        pageResult.setTotal(count);
        return pageResult;
    }
}
